package com.zpark.interceptor;
import org.springframework.web.servlet.HandlerInterceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @author xiaoyu
 * @version 1.0 2022/9/20
 *
 * 自定义拦截器
 */
public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //自定义拦截规则
        //检测session中是否有绑定用户登录信息
        //获取session对象
        HttpSession session = request.getSession();
        //取数据
        if(session.getAttribute("user") != null) {
            //用户已经登录过，放行
            return true;
        } else {
            //用户未登录，不放行
            //重定向到登录页面
            response.sendRedirect("http://localhost:8080/login.html");
            return false;
        }

    }
}
